import React, { useEffect, useRef } from 'react';
import ReactDOM from 'react-dom';

const Portal = ({ children, container }) => {
  const elRef = useRef(document.createElement('div'));

  useEffect(() => {
    const containerEl = container || document.body;
    containerEl.appendChild(elRef.current);
    return () => containerEl.removeChild(elRef.current);
  }, [container]);

  return ReactDOM.createPortal(children, elRef.current);
};

export default Portal;
